package com.thingple.uhf;

import android.app.Activity;
import android.content.Context;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;

import com.thingple.eye.ActionResultListener;
import com.thingple.eye.BaseActivity;
import com.thingple.eye.ViewUtil;


/**
 * RFID_SDK
 * Created by lism on 2017/7/26.D
 */
public class RFIDDeviceManager {

    private static final String INVENTORY_PERMISSION = "com.thingple.tag.permission.inventory";
    private static final String INVENTORY_ACTION = "com.thingple.tag.inventory";

    private static final String driverPath = "com.thingple.tagservice";

    private static final String inventoryPath = "com.thingple.tagservice.view.InventoryActivity";

//    private static final String checkDevicePath = "com.thingple.tagservice.view.StatusActivity";

    private static final String readCardPath = "com.thingple.tagservice.view.ReadCardActivity";

    private static final String writeCardPath = "com.thingple.tagservice.view.WriteCardActivity";

    private static RFIDDeviceManager ins = null;

    private static final Object lock = new Object();

    private TagInfoReciever tagInfoReciever;

    public static RFIDDeviceManager shareInstance() {
        return ins;
    }

    public static void init(Context application) {
        if (ins == null) {
            synchronized (lock) {
                ins = new RFIDDeviceManager(application);
            }
        }
    }

    private RFIDDeviceManager(Context application) {

        if (application == null) {
            throw new RuntimeException("application is null");
        }
        this.tagInfoReciever = new TagInfoReciever();

        Log.d(getClass().getName() + "#RFIDDeviceManager", "初始化Inventory receiver");
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(INVENTORY_ACTION);
        application.registerReceiver(this.tagInfoReciever, intentFilter, INVENTORY_PERMISSION, null);
    }

//    /**
//     * 盘点模式打开设备<br/>
//     * 同步返回方式使用Intent接收数据<br/>
//     * 需要为Context必须为Activity,并设置onActivityResult
//     * @see Context
//     * @see android.app.Activity
//     */
//    public void startIventory(Activity context, Bundle params) {
//        ViewUtil.openViewForResponse(context, driverPath, inventoryPath, params);
//    }

    /**
     * 盘点模式打开设备<br/>
     * 异步返回方式使用session控制设备关闭,设备界面直接返回,通过broadcast接收数据<br/>
     * @param context app
     * @param session 心跳
     */
    public void startIventoryAsync(Context context, DeviceSession session, InventoryListener listener, Bundle params) {

        if (context instanceof BaseActivity) {
            BaseActivity activity = (BaseActivity) context;
            // 激活心跳功能
            // 打开设备界面
            this.tagInfoReciever.registerListener(listener);
            session.start();
            ViewUtil.openViewForResponse(activity, driverPath, inventoryPath, params);
        }
    }

    /**
     * 停止盘点
     * @param session session
     */
    public void stopInventory(DeviceSession session) {

        if (session != null) {
            this.tagInfoReciever.clearListener();
            session.stop();
        }
    }

    /***
     * 写卡<br/>
     * 打开模块写卡,activityResult返回结果
     * @param context activity
     * @param listener listener
     * @param params params
     */
    public void writeCardForResponse(Activity context, ActionResultListener listener, Bundle params) {

        if (context instanceof BaseActivity) {
            BaseActivity activity = (BaseActivity) context;
            // 打开writeCard view,
            ViewUtil.openViewForResponse(activity, driverPath, writeCardPath, params, listener);
        }
    }


    public void readCardForResponse(Activity context, ActionResultListener listener, Bundle params) {
        if (context instanceof BaseActivity) {
            BaseActivity activity = (BaseActivity) context;
            ViewUtil.openViewForResponse(activity, driverPath, readCardPath, params, listener);
        }
    }
}
